package com.shb.game;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Vector;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class GuessNumActivity extends BaseActivity implements OnItemClickListener {
	private Button btnBegin;
	private Button btnHelp;
	private Spinner spSelect;
	private ListView lvResult;
	private GridView gridInput;
	private TextView tvInput;
	/** 目标数字串 */
	private String desString;
	/** 结果适配器 */
	private BaseAdapter resultAdapter;
	/** 输入适配器 */
	private BaseAdapter inputAdapter;
	/** 结果集 */
	private final List<ResultItem> lstResult = new ArrayList<ResultItem>();
	/** 输入字符 */
	private final Vector<String> stInput = new Vector<String>();
	/** 要猜数字的位数 */
	private int num = 3;
	/** 输入字符 */
	private final String[] strInput = new String[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "删", "猜" };
	/** 选关 */
	private final String[] select = new String[] { "3个（简单）", "4个（一般）", "5个（普通）", "6个（较难）", "7个（困难）", };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.guess_num);
		btnBegin = (Button) findViewById(R.id.btnBegin);
		btnHelp = (Button) findViewById(R.id.btnHelp);
		lvResult = (ListView) findViewById(R.id.lvResult);
		gridInput = (GridView) findViewById(R.id.gridInput);
		tvInput = (TextView) findViewById(R.id.tvInput);
		spSelect = (Spinner) findViewById(R.id.spSelect);

		btnBegin.setOnClickListener(this);
		btnHelp.setOnClickListener(this);

		resultAdapter = new ArrayAdapter<ResultItem>(GuessNumActivity.this, 0, lstResult) {
			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				Item it;
				if (convertView == null) {
					convertView = ((LayoutInflater) GuessNumActivity.this.getSystemService("layout_inflater")).inflate(
							R.layout.guessnum_item, null);
					it = new Item();
					it.tvIndex = (TextView) convertView.findViewById(R.id.tvIndex);
					it.tvContent = (TextView) convertView.findViewById(R.id.tvContent);
					it.tvResult = (TextView) convertView.findViewById(R.id.tvResult);
					convertView.setTag(it);
				} else {
					it = (Item) convertView.getTag();
				}
				ResultItem ri = lstResult.get(position);
				it.tvIndex.setText(ri.index);
				it.tvContent.setText(ri.content);
				it.tvResult.setText(ri.result);
				return convertView;
			}

			class Item {
				public TextView tvIndex;
				public TextView tvContent;
				public TextView tvResult;
			}
		};
		lvResult.setAdapter(resultAdapter);
		inputAdapter = new ArrayAdapter<String>(GuessNumActivity.this, 0, strInput) {
			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				TextView btn = new TextView(GuessNumActivity.this);
				btn.setText(strInput[position]);
				btn.setTextColor(Color.BLACK);
				btn.setGravity(Gravity.CENTER);
				btn.setBackgroundResource(R.drawable.btn_bg);
				return btn;
			}
		};
		gridInput.setAdapter(inputAdapter);
		gridInput.setOnItemClickListener(this);

		spSelect.setAdapter(new ArrayAdapter(GuessNumActivity.this, android.R.layout.simple_spinner_item, select));
		spSelect.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> paramAdapterView, View paramView, int paramInt, long paramLong) {
				num = paramInt + 3;
				initDate();
			}

			@Override
			public void onNothingSelected(AdapterView<?> paramAdapterView) {
			}

		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnBegin:
			initDate();
			break;
		case R.id.btnHelp:
			startActivity(new Intent(this, GuessNumHelpActivity.class));
			break;
		}
	}

	/**
	 * 初始化目标
	 * 
	 * @param i
	 */
	private void initDate() {
		desString = "";
		List<String> lst = new ArrayList<String>();
		for (int j = 0; j < 10; j++) {
			lst.add(j + "");
		}
		int n = num;
		while (n > 0) {
			int r = new Random().nextInt(lst.size());
			desString += lst.get(r);
			lst.remove(r);
			n--;
		}
		stInput.clear();
		inputAdapter.notifyDataSetChanged();
		tvInput.setText("");
		lstResult.clear();
		lstResult.add(new ResultItem("序号", "内容", "结果"));
		resultAdapter.notifyDataSetChanged();
	}

	@Override
	public void onItemClick(AdapterView<?> paramAdapterView, View paramView, int paramInt, long paramLong) {
		if (paramInt < 10) {
			if (stInput.size() < num) {
				if (!stInput.contains(strInput[paramInt])) {
					stInput.add(strInput[paramInt]);
				}
			}
		} else if (paramInt == 10) {
			if (stInput.size() > 0) {
				stInput.remove(stInput.size() - 1);
			}
		} else if (paramInt == 11) {
			if (stInput.size() == num) {
				guess();
			}
		}
		inputAdapter.notifyDataSetChanged();
		showInput();
	}

	/** 确认输入，显示结果 */
	private void guess() {
		String input = "";
		for (int i = 0; i < stInput.size(); i++) {
			input += stInput.get(i);
		}
		int da = 0;
		int db = 0;
		for (int i = 0; i < desString.length(); i++) {
			for (int j = 0; j < input.length(); j++) {
				if (desString.charAt(i) == input.charAt(j)) {
					db++;
					if (i == j) {
						da++;
					}
				}
			}
		}
		lstResult.add(new ResultItem(lstResult.size() + "", input, da + "A" + (db - da) + "B"));
		resultAdapter.notifyDataSetChanged();
		lvResult.setSelection(lstResult.size() - 1);
		stInput.clear();
		if (da == num) {
			Toast.makeText(this, "恭喜你用了" + (lstResult.size() - 1) + "次猜对了！", 0).show();
			initDate();
		}

	}

	/** 显示输入结果 */
	private void showInput() {
		String input = "";
		for (int i = 0; i < stInput.size(); i++) {
			input += stInput.get(i);
		}
		tvInput.setText(input);
	}

	class ResultItem {
		public String index;
		public String content;
		public String result;

		public ResultItem(String a, String b, String c) {
			index = a;
			content = b;
			result = c;
		}
	}
}
